package util;

import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * 上传图片格式大小检查
 * @author oahner
 * @create 2022-04-20 14:09
 */
public class ImgUtil {


    public static Map<String,Object> imgChecked(MultipartFile mul){
        Map map = new HashMap();
        map.put("flag",false);
        map.put("msg","file checked is fail");
        if(null!=mul.getOriginalFilename() && !"".equals(mul.getOriginalFilename())){
            String contentType = mul.getContentType();
            if(!contentType.equals("image/jpeg")){
                map.put("msg","图片格式需为image/jpeg,你的文件格式："+contentType);
            }else if(mul.getSize()/1024/1024 > 2){
                map.put("msg","图片大小不能大于2MB，你的图片大小："+mul.getSize()/1024/1024+"MB");
            }else{
                map.put("flag",true);
                map.put("msg","Img Checked success");
            }
        }else{
            map.put("flag",false);
            map.put("msg","file is empty");
        }
        return map;
    }



}
